package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.service.IUserOperationLogService;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;

/**
 * <desc>
 *     保存开发者访问日志消息队列消费者
 * </desc>
 *
 * @createDate 2018/12/19
 */
@Component
public class DeveloperLogConsumer2 {

    private static final Logger LOG = LoggerFactory.getLogger(DeveloperLogConsumer2.class);

    @Autowired
    private IUserOperationLogService iUserOperationLogService;

    /**
     * <desc>
     *     保存开发者访问日志消息队列消费过程
     * </desc>
     *
     * @param message 监听到的业务消息
     * @param channel 频道
     * @author wangzhi
     * @createDate 2018/12/19
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_LOG_SAVE_QUEUE)
    public void process(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),TimeTaskDeadMessageQueuePO.class);
            String content = timeTaskDeadMessageQueuePO.getMessageContent();
            JSONObject jsdata = JSONObject.parseObject(content);
            String data = jsdata.getString("data");
            HashMap map = JsonUtil.toObject(data, HashMap.class);
            if (map.containsKey("ip")) {
                map.put("ipAddress", HttpProtocolUtil.getIpAddressFromBaidu(map.get("ip").toString()));
            }
//            HashMap map = jsdata.getObject("data", HashMap.class);
            iUserOperationLogService.saveDeveloperLog(map);
        } catch (Exception e) {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),TimeTaskDeadMessageQueuePO.class);
            String content = timeTaskDeadMessageQueuePO.getMessageContent();
            JSONObject jsdata = JSONObject.parseObject(content);
            String data = jsdata.getString("data");
            HashMap map = JsonUtil.toObject(data, HashMap.class);
            if (map.containsKey("ip")) {
                map.put("ipAddress", HttpProtocolUtil.getIpAddressFromBaidu(map.get("ip").toString()));
            }
            LOG.error("【RabbitMQ_保存开发者访问日志消费端】:入参为============>{}",map.toString());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_保存开发者访问日志消费端】:消费异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}
